/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import biz.NewsEventBO;
import entity.NewsEvent;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Random;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 *
 * @author Admin
 */
public final class MTimerTask extends TimerTask {

    private JLabel display;
    private JLabel pic;
    private ArrayList<NewsEvent> arr;
    private int curIndex;
    private final Main root;

    public MTimerTask(Main frame, JLabel display, JLabel pic) {
        this.root = frame;
        this.display = display;
        try {
            this.arr = new NewsEventBO().getAllNewsEvent();
        } catch (SQLException ex) {
            Logger.getLogger(MTimerTask.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.pic = pic;

        pic.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    NewsEvent data = arr.get(curIndex);
                    DialogNewEvents dialog;
                    switch (root.getCurAccLv()) {
                        case 0:
                        case -1:
                        case 1:
                        default:
                            dialog = new DialogNewEvents(root, false, DialogNewEvents.SHOW, data);
                            break;
                        case 2:
                            dialog = new DialogNewEvents(root, false, DialogFitness.EDIT, data);
                            break;
                    }
                    root.setupDialog(dialog);
                }
            }
        });
    }
    public void load()
    {
        try {
            this.arr = new NewsEventBO().getAllNewsEvent();
        } catch (SQLException ex) {
            Logger.getLogger(MTimerTask.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public int getCurIndex() {
        return curIndex;
    }

    @Override
    public void run() {
        if (arr.size() < 1) {
            cancel();
        }
        curIndex = (new Random()).nextInt(arr.size());
        display.setText(arr.get(curIndex).toString());
        pic.setIcon(new ImageIcon(((NewsEvent) (arr.get(curIndex))).getImage().getScaledInstance(pic.getWidth(), -1, Image.SCALE_REPLICATE)));
    }
}
